package com.cloud.admin.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.cloud.admin.model.entity.SysDictData;
import com.cloud.admin.model.entity.SysMenu;
import com.cloud.admin.model.entity.SysUser;
import com.cloud.admin.service.SysMenuService;
import com.cloud.admin.service.impl.DictService;
import com.cloud.admin.shiro.security.ShiroUtils;
import com.cloud.common.security.AES;
import com.cloud.common.shiro.model.User;
import com.cloud.common.util.CookieUtils;
import com.cloud.common.util.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping()
public class HomeController extends BaseController {

    private final SysMenuService sysMenuService;
    private final DictService dictService;

    @PostMapping("setplatformcookie")
    @ResponseBody
    private R setPlatformCookie(@Validated SysDictData platform, HttpServletRequest request, HttpServletResponse response) {
        CookieUtils.setCookie(
                request, response, "platform",
                AES.aesEncrypt(JSONUtil.toJsonStr(platform)),
                60 * 60 * 24, false, null, "/");
        return new R();
    }

    @GetMapping("index")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response, Model model) {
        // 取身份信息
        User user = ShiroUtils.getSysUser();

        SysDictData defaultPlatform = null;
        //获取Cookie里存的平台信息
        String platformCookie = CookieUtils.getCookie(request, response, "platform");
        if (!StrUtil.isEmpty(platformCookie)) {
            defaultPlatform = JSONUtil.toBean(AES.aesDecrypt(platformCookie), SysDictData.class);
        }

        // 根据用户id取出菜单
        SysUser sysUser = new SysUser();
        sysUser.setUserId(user.getUserId());

        List<SysDictData> platformList = dictService.selectPlatform(sysUser);
        if (defaultPlatform == null) {
            defaultPlatform = platformList.get(0);
            CookieUtils.setCookie(request, response, "platform", AES.aesEncrypt(JSONUtil.toJsonStr(defaultPlatform)));
        }

        List<SysMenu> menus = sysMenuService.selectMenusByUser(sysUser, Integer.valueOf(defaultPlatform.getDictValue()));

        ModelAndView view = super.baseView("", "index");
        view.addObject("menus", menus);
        user.setAvatar(config.getImgUrl() + (StrUtil.isEmpty(user.getAvatar()) ? config.getInitAvatar() : user.getAvatar()));

        view.addObject("user", user);
        view.addObject("platformList", platformList);
        view.addObject("defaultPlatform", defaultPlatform);
        view.addObject("imgUrl", config.getImgUrl());
        view.addObject("yaoxiName", config.getYaoxiName());
        view.addObject("yaoxiSystemName", config.getYaoxiSystemName());
        return view;
    }

    @GetMapping("system/main")
    public ModelAndView main() {
        ModelAndView view = super.baseView("main", "main_v1");
        return view;
    }
}
